<?php

defined('BASEPATH') or die('forbidden');

class Index_Controller Extends Controller {

    function index() {
        if (stripos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== FALSE) {
            //show_message("{$this->config['sitename']}后台管理不支持使用IE核心的浏览器,请使用基于Webkit核心(Firefox、Chrome、Safari等)的浏览器重试", "javascript:;", 0, 'help');
        }
        check_level(50);
        $data = array();
        $this->view->display("index/index", $data);
    }

    function left() {
        check_level(50);
        $this->view->display("index/left");
    }

    function login() {
        if ($this->auth->check_level(50, null, null, null, true))
            error(translate('INDEX_DO_NOTLOGIN_AGAIN'), site_url("index"), 1000);
        if (is_submit()) {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $url = $this->input->post('url');
            $url = $url == '' ? site_url('index') : $url;
            if ($this->auth->login($username, $password)) {
                insert_log(translate('INDEX_NO_PERMISSION', $this->auth->user_id));
                if ($this->auth->role_rank >= 50) {
                    $_SESSION['is_admin'] = md5s($this->auth->fields['id'] . $this->auth->fields['sessionid']);
                    $this->cookie->set("is_admin", md5s($this->auth->fields['id'] . $this->auth->fields['sessionid']), 0, base_url());
                    go($url);
                } else {
                    $this->_logout();
                    error(translate('INDEX_NO_PERMISSION'));
                }
            } else {
                insert_log(translate('INDEX_NO_PERMISSION', $this->auth->error));
                $this->_logout();
                error($this->auth->error);
            }
        }
        $this->view->assign("url", $this->input->get("url"));
        $this->view->display("index/login");
    }

    function logout() {
        $this->_logout();
        header("location:" . site_url('index'));
    }

    function _logout() {
        $this->auth->logout(base_url());
        $this->cookie->clear('user_id', base_url());
        $this->cookie->clear('is_admin', base_url());
        $this->cookie->clear('safecode', base_url());
        $this->cookie->clear('username', base_url());
        $this->cookie->clear('sessionid', base_url());
    }

}